$import("com.hh.workflow.designer.WorkFlowListTree");
Ext
		.define(
				'com.hh.workflow.start.StartList',
				{
					extend : 'com.hh.global.SimpleGridPanelWindow',
					action : 'workflow-Start-',
					// editPage : 'com.hh.usersystem.user.UserEdit',
					constructor : function(config) {
						this.config = config || {};
						this.superclass.constructor.call(this, this.config);
						this.init();
						this.panel.add(this.createWorkFlowListTree());
					},
					gridConfig : {
						border : 1,
						padding : '1',
						title : "我的流程"
					},
					getSearchPanelItems : function() {
						return []
					},
					getTbarItems : function() {
						var page = this;
						return [ this.getMyToolbarItem("start"),
								this.getMyToolbarItem("select"),
								this.getMyToolbarItem("end"),
								this.getToolbarItem("delete"),
								this.getMyToolbarItem("selectAll") ];
					},
					getMyToolbarItem : function(type) {
						var page = this;
						if (type == 'start') {
							return {
								iconCls : 'add',
								text : '新建工作',
								handler : function() {
									page.doStart();
								}
							};
						} else if (type == 'end') {
							return {
								iconCls : 'delete2',
								text : '终止',
								handler : function() {
									page.doEnd();
								}
							};
						} else if (type == 'select') {
							return {
								iconCls : 'search',
								text : '查看历史',
								handler : function() {
									page.doSelectHi();
								}
							};
						} else if (type == 'selectAll') {
							return {
								iconCls : 'search',
								text : '查看所有流程',
								handler : function() {
									page.doSelectAll();
								}
							};
						}
					},
					doSelectAll : function() {
						var page = this;
						Ext.apply(page.grid.getStore().proxy.extraParams, {
							pdkey : null
						});
						page.grid.getStore().load();
					},
					getContainerRightMenuItems : function() {
						var toolbarItems = [];
						toolbarItems.push(this.getMyToolbarItem("start"));
						toolbarItems.push(this.getMyToolbarItem("selectAll"));
						return toolbarItems;
					},
					getRightMenuItems : function() {
						var toolbarItems = [];
						toolbarItems.push(this.getMyToolbarItem("start"), this
								.getMyToolbarItem("select"), this
								.getMyToolbarItem("end"), this
								.getToolbarItem("delete"), this
								.getMyToolbarItem("selectAll"));
						return toolbarItems;
					},
					doSelectHi : function() {
						var record = this.getGridRecord();
						if (record == null) {
							ExtFrame.info("请选中您要查看历史的流程！！");
							return;
						}
						var win = ExtUtil.create(
								'com.hh.workflow.task.HiTaskList', {
									id : this.id
											+ '-'
											+ 'com.hh.workflow.task.HiTaskList'
													.replace(/\./g, ''),
									parentPanel : this,
									extraParams : {
										piid : record.get("id"),
										pdid : record.get("pdid")
									}
								});
						win.show();
					},
					doEnd : function() {
						var records = this.getGridRecords();
						var isDelete = true;
						for ( var i = 0; i < records.length; i++) {
							if (records[i].get("endTime")) {
								isDelete = false;
								break;
							}
						}
						if (isDelete) {
							this.doDelete("end", '您确定要终止流程吗？');
						} else {
							ExtFrame.msg("您选中的流程已经结束了，不能终止！");
						}

					},
					doStart : function() {
						var page = this;
						if (Util.isNull(this.pdkey)) {
							ExtFrame.info("请选中您要新建的流程！！");
						} else {
							var result = Request.synRequestObject(
									'workflow-Start-start', {
										pdkey : this.pdkey
									});
							if (result.href) {
								var params = {};

								var callbackRefresh = function() {
									page.grid.getStore().load();
								};

								Ext
										.apply(params, Ext
												.decode(result.hrefParams));
								params.objectId = result.objectId;
								params.taskId = result.taskId;
								params.piid = result.piid;
								params.pdid = result.pdid;
								params.activityId = result.activityId;
								params.title = result.pdName + "（"
										+ result.name + "）";
								params.pdName = result.pdName;
								params.actName = result.name;
								params.callbackRefresh = callbackRefresh;
								params.actConfig = result;
								params.actionType = "manager";
								Desktop.openWindow(result.href, params);
							}
						}
					},
					getGridColumns : function() {
						return [
								{
									dataIndex : 'deploymentId',
									hidden : true,
									flex : 1
								},
								{
									dataIndex : 'pdid',
									hidden : true,
									flex : 1
								},
								{
									text : '状态',
									dataIndex : 'endTime',
									width : 50,
									renderer : function(value) {
										if (Util.isNull(value)) {
											return '<font color=green>执行中</font>'
										} else {
											return '<font color=red>已结束</font>'
										}
									}
								},
								{
									text : '流水号',
									dataIndex : 'id',
									flex : 1
								},
								{
									text : '流程名称',
									dataIndex : 'pdName',
									flex : 1
								},
								{
									text : '流程开始时间',
									dataIndex : 'startTime',
									width : 200,
									renderer : Ext.util.Format
											.dateRenderer('Y年m月d日 H时i分s秒')
								},
								{
									text : '流程结束时间',
									dataIndex : 'endTime',
									width : 200,
									renderer : Ext.util.Format
											.dateRenderer('Y年m月d日 H时i分s秒')
								}, {
									text : '耗时',
									dataIndex : 'durationInMillis',
									width : 120
								} ];
					},
					getStoreFields : function() {
						return [ 'id', 'pdid', 'startTime', 'endTime',
								'pdName', 'durationInMillis', 'deploymentId' ];
					},
					createWorkFlowListTree : function() {
						var page = this;
						var workFlowListTree = this.workFlowListTree = Ext
								.create(
										'com.hh.workflow.designer.WorkFlowListTree',
										{
											editPage : null,
											addToolbarItems : function() {
											},
											getTreePanelListeners : function() {
												return {
													itemclick : function(view,
															record, item) {
														if (record.get('leaf') == false) {
															return;
														}
														page.grid
																.setTitle("我的流程"
																		+ "<font color=red>（"
																		+ record
																				.get("text")
																		+ "）</font>");
														var object = Request
																.synRequestObject(
																		'workflow-Start-findWorkFlowByDataId',
																		{
																			dataId : record
																					.get("id")
																		});
														if (object == null) {
															page.grid
																	.getStore()
																	.removeAll();
														} else {
															page.pdkey = object.id;
															Ext
																	.apply(
																			page.grid
																					.getStore().proxy.extraParams,
																			{
																				pdkey : page.pdkey
																			});
															page.grid
																	.getStore()
																	.load();
														}
													}
												}
											}
										});
						return workFlowListTree;
					}
				});